package com.archeanx.libx.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.archeanx.libx.widget.R;

public class XLoadingDialog extends Dialog {

    private String mDialogContent;
    /**
     * 后退键关闭弹框
     */
    private boolean isCancelable = true;
    /**
     * 点击屏幕关闭弹框
     */
    private boolean isCanceledOnTouchOutside = false;
    private TextView mProgressContent;

    public static XLoadingDialog show(Context context, String message) {
        XLoadingDialog xLoadingDialog = new XLoadingDialog(context, message);
        xLoadingDialog.show();
        return xLoadingDialog;
    }


    public XLoadingDialog(@NonNull Context context) {
        this(context, "");
    }

    public XLoadingDialog(@NonNull Context context, String message) {
        super(context, R.style.lw_loading_dialog);
        this.mDialogContent = message;
    }


    /**
     * @param context                context
     * @param message                消息
     * @param cancelable             后退键关闭弹框
     * @param canceledOnTouchOutside 点击屏幕关闭弹框
     */
    public XLoadingDialog(@NonNull Context context, String message, boolean cancelable, boolean canceledOnTouchOutside) {
        super(context, R.style.lw_loading_dialog);
        this.mDialogContent = message;
        this.isCancelable = cancelable;
        this.isCanceledOnTouchOutside = canceledOnTouchOutside;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View dialogView = LayoutInflater.from(getContext()).inflate(R.layout.lw_dialog_progress_dialog, null);
        setContentView(dialogView);
        mProgressContent = dialogView.findViewById(R.id.lw_progress_content);
        mProgressContent.setText(mDialogContent);
        setCancelable(isCancelable);
        setCanceledOnTouchOutside(isCanceledOnTouchOutside);
    }


    @Override
    public void show() {
        super.show();
    }



    public void show(String message) {
        mDialogContent=message;
        if (mProgressContent != null) {
            mProgressContent.setText(message);
        }
        show();
    }
}
